﻿using System.Diagnostics;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Data.OleDb;

namespace Ooaavee.Data.Model
{
    /// <summary>
    /// A data type.
    /// </summary>
    [DataContract]
    [DebuggerDisplay("{DebuggerDisplay,nq}")]
    public sealed class DataType
    {
        private int? _hashCode;

        /// <summary>
        /// The provider-specific type value that should be used when specifying a parameter’s type. For example, SqlDbType.Money or OracleType.Blob.
        /// </summary>
        [DataMember]
        public int ProviderDbType { set; get; }

        /// <summary>
        /// The OleDb data type.
        /// </summary>
        [DataMember]
        public OleDbType OleDbType { set; get; }

        /// <summary>
        /// The provider-specific data type name.
        /// </summary>
        [DataMember]
        public string Name { set; get; }

        /// <summary>
        /// The name of the .NET Framework type of the data type.
        /// </summary>
        [DataMember]
        public string FrameworkType { set; get; }

        /// <summary>
        /// Determines whether the specified object is equal to the current DataType.
        /// </summary>
        /// <param name="obj">The object to compare with the current DataType.</param>
        /// <returns>true if the specified object is equal to the current DataType; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            var equals = false;
            var other = obj as DataType;
            if (other != null)
            {
                equals = GetHashCode().Equals(other.GetHashCode());
            }
            return equals;
        }
        
        /// <summary>
        /// Serves as a hash function for the DataType class.
        /// </summary>
        /// <returns>A hash code for the current DataType.</returns>
        public override int GetHashCode()
        {
            if (_hashCode == null)
            {
                _hashCode = string.Format("{0}-{1}-{2}", OleDbType, Name, FrameworkType).GetHashCode();
            }
            return _hashCode.Value;
        }

        #region Debugger
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "See DebuggerDisplayAttribute marked on this class.")]
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string DebuggerDisplay { get { return string.Format("{0} / {1} / {2}", Name, OleDbType, FrameworkType); } }
        #endregion
    }
}
